package com.flutter_ecg.flutter_ecg.ecgapplication.utils;


import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.net.Uri;
//import android.support.annotation.RawRes;
import androidx.annotation.RawRes;
import android.util.Log;

import java.io.IOException;

public class VoicePlayMp3 {
    private MediaPlayer mediaPlayer;
    private Context context;
    private AssetManager assetManager;
    private final String TAG = "VoicePlayMp3";
    private String format = ".mp3";
    private boolean isCanPlayVoice = true;
    private OnCompletionListener onCompletionListener;

    public VoicePlayMp3(Context context) {
        this.context = context;
        assetManager = context.getAssets();
        initPlayer();
    }


    private void initPlayer() {
        if (null == mediaPlayer) {
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    try {
                        if (null != onCompletionListener) {
                            onCompletionListener.onCompletion(mp);
                        }
                        mp.reset();
                    } catch (IllegalStateException e) {
                        mediaPlayer = null;
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                        mediaPlayer = null;
                    } catch (SecurityException e) {
                        e.printStackTrace();
                        mediaPlayer = null;
                    }
//                    mp.release();
                }
            });
        }
    }

    public void setTag(boolean isCanPlayVoice) {
        this.isCanPlayVoice = isCanPlayVoice;
    }

    public void playVoice(String name) {
        if (!isCanPlayVoice) return;
        initPlayer();

//        modify by lierqi start
//        if (mediaPlayer.isPlaying()) {
        if (mediaPlayer != null) {//取消isPlaying判断，似乎不靠谱
//        modify by lierqi end
            mediaPlayer.stop();
            mediaPlayer.reset();
//            mediaPlayer.release();
        }
        try {
//            String name = context.getResources().getResourceName(resid);
            Log.i(TAG, "<-------------->" + name + format);
            AssetFileDescriptor fileDescriptor = assetManager.openFd(name + format);
            mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(), fileDescriptor.getLength());
            mediaPlayer.prepare();
            mediaPlayer.start();
        } catch (IOException e) {
            e.printStackTrace();
            mediaPlayer = null;
        } catch (IllegalStateException e) {
            mediaPlayer = null;
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            mediaPlayer = null;
        } catch (SecurityException e) {
            e.printStackTrace();
            mediaPlayer = null;
        }
    }

    public void playVoice(@RawRes int name) {
        playVoice(name, null);
    }

    public void playVoice(@RawRes int name, OnCompletionListener onCompletionListener) {
        if (!isCanPlayVoice) {
            onCompletionListener.onCompletion(null);
            return;
        }
        initPlayer();
//        modify by lierqi start
//        if (mediaPlayer.isPlaying()) {
        if (mediaPlayer != null) {//取消isPlaying判断，似乎不靠谱
//        modify by lierqi end
            mediaPlayer.stop();
            mediaPlayer.reset();
//            mediaPlayer.release();
        }
        try {
//            String name = context.getResources().getResourceName(resid);
            this.onCompletionListener = onCompletionListener;
            Log.i(TAG, "<-------------->" + name + format);
            mediaPlayer.setDataSource(context, resourceIdToUri(context, name));
            mediaPlayer.prepare();
            mediaPlayer.start();
        } catch (IOException e) {
            e.printStackTrace();
            mediaPlayer = null;
        } catch (IllegalStateException e) {
            mediaPlayer = null;
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            mediaPlayer = null;
        } catch (SecurityException e) {
            e.printStackTrace();
            mediaPlayer = null;
        }
    }

    public static final String ANDROID_RESOURCE = "android.resource://";
    public static final String FOREWARD_SLASH = "/";

    private static Uri resourceIdToUri(Context context, int resourceId) {
//        Uri.parse("android.resource://"+AppUtils.getAppPackageName(context)+"raw"
        return Uri.parse(ANDROID_RESOURCE + context.getPackageName() + FOREWARD_SLASH + resourceId);
    }

    public void onPause() {
        if (mediaPlayer != null) {
//            modify by lierqi start
//            if (mediaPlayer.isPlaying()) {
                mediaPlayer.stop();
                mediaPlayer.reset();
            }
//        }
//            modify by lierqi start
    }

    public interface OnCompletionListener {
        void onCompletion(MediaPlayer mp);
    }

}
